#!/usr/bin/env python
#_*_coding:utf-8 _*_
__author__ = 'silence'
import datetime
import file_operate

#定义加密模块
def md5(str):
    import hashlib
    import types
    if type(str) is types.StringType:
        m = hashlib.md5()
        m.update(str)
        return m.hexdigest()
    else:
        return ''

#传入用户名和密码，验证用户登录
def login_verification(username,password):
    user_info = file_operate.file_read("user_info.pkl")
    if username not in user_info:
        info = -1 #-1代表用户不存在
        return False,info
    elif username in user_info:
        if user_info[username][1] == "locked":
            info = 0 #0代表用户锁定
            return False,info
        elif user_info[username][0] == md5(password) and user_info[username][1] != "locked":
            info = user_info[username][2] #代表用户正常登录,返回用户类型
            return True,info
        else:
            info = 2
            return False,info

#锁定用户模块
def lock_user(username):
    user_info = file_operate.file_read("user_info.pkl")
    if username in user_info:
        user_info[username][1] = "locked"
        file_operate.file_write(user_info,"user_info.pkl")
        return True
    else:
        return False

#调用用户登录模块，打印登录信息，此为登录调用接口，返回登录名和用户级别
def login_control():
    retry_count = 0
    temp_list = []
    while retry_count < 3:
        input_name = raw_input(u"请输入您的用户名:").strip()
        retry_count += 1
        temp_list.append(input_name)
        if len(input_name) ==0:
            print u"空用户名不被允许",
            continue
        input_pwd = raw_input(u"请输入您的密码:")
        login_status = login_verification(input_name,input_pwd)
        if login_status[0]:
            print u"%s,欢迎登录系统......" %input_name
            return input_name,login_status[1]
        elif not login_status[0]:
            if login_status[1] == 0:
                print u"用户已锁定"
                break
            elif login_status[1] == -1:
                print u"用户不存在"
                continue
            elif login_status[1] == 2:
                print u"用户名密码不正确"
                continue
    user_info = file_operate.file_read("user_info.pkl")
    if retry_count == 2:
        if temp_list[0]==temp_list[1] == input_name and input_name in user_info:
            if user_info[input_name] != "admin":
                locked = lock_user(input_name)
                if locked:
                    print u"%s,你输入错误三次密码，账户已被锁定" %input_name


#注册用户模块
def registered(user="user"):
    user_info = file_operate.file_read("user_info.pkl")
    username = raw_input(u"请输入用户名：").strip()
    if len(username) < 4:
        #print u"用户名太短，请输入至少4个字符!"
        return -1 #-1代表不符合长度
    elif username in user_info:
        #print u"用户已存在"
        return 0
    else:
        password = raw_input(u"请输入你的密码:")
        retry_password = raw_input(u"请在次输入密码:")
        if password == retry_password:
            create_date = datetime.datetime.now()
            #定义列表第一字段为密码，第二字段为locked状态，第三字段为用户级别，第四字段为资金，第五字段为创建时间
            user_info[username] = [md5(password),"",user,15000.00,0.00,create_date]
            file_operate.file_write(user_info,"user_info.pkl")
            return 1 #成功添加
        elif password != retry_password:
            return 2 # 2次密码不匹配

#调用用户注册模块，打印注册信息，此为调用接口
def register_status_info():
    regi_status = registered()
    if regi_status == 0:
        print u"\033[1;32;40m用户已存在!\033[0m"
    elif regi_status == -1:
        print u"\033[5;31;40m用户名太短，请重新注册！\033[0m"
    elif regi_status ==1:
        print u"\033[1;32;40m注册成功\033[0m"
    elif regi_status ==2:
        print u"\033[5;31;40m2次输入的密码不匹配\033[0m"
